From 213f9acccb725a7ddcd9f41eee9d5234d81e1dc0 Mon Sep 17 00:00:00 2001
From: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
Date: Fri, 25 Mar 2022 10:18:40 -0300
Subject: [PATCH 1/2] [libcxx] Rename PS() macro to avoid clashing with Xtensa
 register name

This patch addresses a clash with the PS register from Xtensa
defined in the <specreg.h> header file, which is commonly
included in OS implementation.

Issue identified while building libc++ port for Apache NuttX, targeting
Xtensa-based chips (e.g. Espressif's ESP32).

Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>

Differential Revision: https://reviews.llvm.org/D122479
---
 libcxx/src/filesystem/filesystem_common.h |  4 +-
 libcxx/src/filesystem/operations.cpp      | 46 +++++++++++------------
 2 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/libcxx/src/filesystem/filesystem_common.h b/libcxx/src/filesystem/filesystem_common.h
index e0fdbccf96b1..b62f1406de39 100644
--- a/libcxx/src/filesystem/filesystem_common.h
+++ b/libcxx/src/filesystem/filesystem_common.h
@@ -41,9 +41,9 @@
 #endif
 
 #if defined(_LIBCPP_WIN32API)
-#define PS(x) (L##x)
+#  define PATHSTR(x) (L##x)
 #else
-#define PS(x) (x)
+#  define PATHSTR(x) (x)
 #endif
 
 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
diff --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp
index 50a895dc2fae..830781def52d 100644
--- a/libcxx/src/filesystem/operations.cpp
+++ b/libcxx/src/filesystem/operations.cpp
@@ -188,14 +188,14 @@ public:
     switch (State) {
     case PS_BeforeBegin:
     case PS_AtEnd:
-      return PS("");
+      return PATHSTR("");
     case PS_InRootDir:
       if (RawEntry[0] == '\\')
-        return PS("\\");
+        return PATHSTR("\\");
       else
-        return PS("/");
+        return PATHSTR("/");
     case PS_InTrailingSep:
-      return PS("");
+      return PATHSTR("");
     case PS_InRootName:
     case PS_InFilenames:
       return RawEntry;
@@ -303,8 +303,8 @@ private:
 };
 
 string_view_pair separate_filename(string_view_t const& s) {
-  if (s == PS(".") || s == PS("..") || s.empty())
-    return string_view_pair{s, PS("")};
+  if (s == PATHSTR(".") || s == PATHSTR("..") || s.empty())
+    return string_view_pair{s, PATHSTR("")};
   auto pos = s.find_last_of('.');
   if (pos == string_view_t::npos || pos == 0)
     return string_view_pair{s, string_view_t{}};
@@ -591,8 +591,8 @@ filesystem_error::~filesystem_error() {}
 void filesystem_error::__create_what(int __num_paths) {
   const char* derived_what = system_error::what();
   __storage_->__what_ = [&]() -> string {
-    const path::value_type* p1 = path1().native().empty() ? PS("\"\"") : path1().c_str();
-    const path::value_type* p2 = path2().native().empty() ? PS("\"\"") : path2().c_str();
+    const path::value_type* p1 = path1().native().empty() ? PATHSTR("\"\"") : path1().c_str();
+    const path::value_type* p2 = path2().native().empty() ? PATHSTR("\"\"") : path2().c_str();
     switch (__num_paths) {
     default:
       return detail::format_string("filesystem error: %s", derived_what);
@@ -1378,7 +1378,7 @@ path& path::replace_extension(path const& replacement) {
   }
   if (!replacement.empty()) {
     if (replacement.native()[0] != '.') {
-      __pn_ += PS(".");
+      __pn_ += PATHSTR(".");
     }
     __pn_.append(replacement.__pn_);
   }
@@ -1500,14 +1500,14 @@ enum PathPartKind : unsigned char {
 static PathPartKind ClassifyPathPart(string_view_t Part) {
   if (Part.empty())
     return PK_TrailingSep;
-  if (Part == PS("."))
+  if (Part == PATHSTR("."))
     return PK_Dot;
-  if (Part == PS(".."))
+  if (Part == PATHSTR(".."))
     return PK_DotDot;
-  if (Part == PS("/"))
+  if (Part == PATHSTR("/"))
     return PK_RootSep;
 #if defined(_LIBCPP_WIN32API)
-  if (Part == PS("\\"))
+  if (Part == PATHSTR("\\"))
     return PK_RootSep;
 #endif
   return PK_Filename;
@@ -1557,7 +1557,7 @@ path path::lexically_normal() const {
         NewPathSize -= Parts.back().first.size();
         Parts.pop_back();
       } else if (LastKind != PK_RootSep)
-        AddPart(PK_DotDot, PS(".."));
+        AddPart(PK_DotDot, PATHSTR(".."));
       MaybeNeedTrailingSep = LastKind == PK_Filename;
       break;
     }
@@ -1572,7 +1572,7 @@ path path::lexically_normal() const {
   }
   // [fs.path.generic]p6.8: If the path is empty, add a dot.
   if (Parts.empty())
-    return PS(".");
+    return PATHSTR(".");
 
   // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any
   // trailing directory-separator.
@@ -1584,7 +1584,7 @@ path path::lexically_normal() const {
     Result /= PK.first;
 
   if (NeedTrailingSep)
-    Result /= PS("");
+    Result /= PATHSTR("");
 
   return Result;
 }
@@ -1593,9 +1593,9 @@ static int DetermineLexicalElementCount(PathParser PP) {
   int Count = 0;
   for (; PP; ++PP) {
     auto Elem = *PP;
-    if (Elem == PS(".."))
+    if (Elem == PATHSTR(".."))
       --Count;
-    else if (Elem != PS(".") && Elem != PS(""))
+    else if (Elem != PATHSTR(".") && Elem != PATHSTR(""))
       ++Count;
   }
   return Count;
@@ -1642,15 +1642,15 @@ path path::lexically_relative(const path& base) const {
     return {};
 
   // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise
-  if (ElemCount == 0 && (PP.atEnd() || *PP == PS("")))
-    return PS(".");
+  if (ElemCount == 0 && (PP.atEnd() || *PP == PATHSTR("")))
+    return PATHSTR(".");
 
   // return a path constructed with 'n' dot-dot elements, followed by the the
   // elements of '*this' after the mismatch.
   path Result;
   // FIXME: Reserve enough room in Result that it won't have to re-allocate.
   while (ElemCount--)
-    Result /= PS("..");
+    Result /= PATHSTR("..");
   for (; PP; ++PP)
     Result /= *PP;
   return Result;
@@ -1662,9 +1662,7 @@ static int CompareRootName(PathParser *LHS, PathParser *RHS) {
   if (!LHS->inRootName() && !RHS->inRootName())
     return 0;
 
-  auto GetRootName = [](PathParser *Parser) -> string_view_t {
-    return Parser->inRootName() ? **Parser : PS("");
-  };
+  auto GetRootName = [](PathParser* Parser) -> string_view_t { return Parser->inRootName() ? **Parser : PATHSTR(""); };
   int res = GetRootName(LHS).compare(GetRootName(RHS));
   ConsumeRootName(LHS);
   ConsumeRootName(RHS);
-- 
2.32.0

